package com.ltxtiyu.video.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.ltxtiyu.video.bean.VideoInfoBean;
import com.ltxtiyu.video.cacheDao.MomentCacheDao;
import com.ltxtiyu.video.cacheDao.VideoCacheDao;
import com.ltxtiyu.video.common.ConfigConstant;

import com.ltxtiyu.video.dao.ltx.ConfigDao;
import com.ltxtiyu.video.dao.ltx.MomentDao;
import com.ltxtiyu.video.dao.ltx.VideoDao;
import com.ltxtiyu.video.dao.sjs.SjsClubDao;
import com.ltxtiyu.video.dao.sjs.SjsVideoDao;
import com.ltxtiyu.video.entity.Moment;
import com.ltxtiyu.video.entity.Video;
import com.ltxtiyu.video.entity.sjs.SjsClub;
import com.ltxtiyu.video.service.ms.SjsService;
import com.ltxtiyu.video.tool.AliVideoTool;
import com.ltxtiyu.video.tool.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SjsServiceImpl implements SjsService {
    @Autowired
    private MomentDao momentDao;

    @Autowired
    private VideoDao videoDao;
    @Autowired
    private SjsVideoDao sjsVideoDao;
    @Autowired
    private SjsClubDao sjsClubDao;

    @Autowired
    private ConfigDao configDao;

    @Autowired
    private MomentCacheDao momentCacheDao;
    @Autowired
    private VideoCacheDao videoCacheDao;

    @Override
    public boolean transferVideo(Integer club, Date startTime, Date endTime, Integer publisher) {
        Map<String,Object> param = new HashMap<>();
        param.put("clubId",club);
        param.put("startTime",startTime);
        param.put("endTime",endTime);
        List<String> aliVideoIdSTR= sjsVideoDao.findVidoeId(param);
        if (aliVideoIdSTR.size() >0 && aliVideoIdSTR != null){
            for (String aliVideoId:aliVideoIdSTR) {
                Video record =   videoDao.findByaliVideoId(aliVideoId);
                if (record == null && StringUtil.isNotBlank(aliVideoId)){
                    //视频信息
                    String accessKeyId = configDao.selectByCkey(ConfigConstant.ACCESSKEYID).getCvalue();
                    String accessKeySecret = configDao.selectByCkey(ConfigConstant.ACCESSKEYSECRET).getCvalue();
                    String vodDomain = configDao.selectByCkey(ConfigConstant.ALIVOD_URL).getCvalue();
                    String alivodPlayAction = configDao.selectByCkey(ConfigConstant.ALIVOD_PLAY_ACTION).getCvalue();
                    AliVideoTool instance = AliVideoTool.getInstance(accessKeyId, accessKeySecret, vodDomain, alivodPlayAction);
                    Map<String, String> videoPlayUrl = instance.getVideoPlayUrl(aliVideoId);
                    if (videoPlayUrl!= null ||!videoPlayUrl.isEmpty() ){
                        VideoInfoBean videoInfoBean = new VideoInfoBean();
                        videoInfoBean.setCoverUrl(videoPlayUrl.get("CoverURL"));
                        videoInfoBean.setDuration(videoPlayUrl.get("Duration"));
                        videoInfoBean.setPlayUrl(videoPlayUrl.get("PlayURL"));
                        //入库
                        Video video = new Video();
                        video.setVideoId(aliVideoId);
                        video.setStatus(1);
                        video.setCreateTime(new Date());
                        video.setMadeTime(new Date());
                        video.setVideoInfo(JSONObject.toJSONString(videoInfoBean));
                        videoCacheDao.insertSelective(video);
                        Moment moment = new Moment();
                        moment.setVideoId(video.getId());
                        moment.setCreateTime(new Date());
                        moment.setPublisher(publisher);
                        moment.setStatus(1);
                        moment.setPermissionType(110);
                        momentCacheDao.insertSelective(moment);
                    }
                }
                }
            return true;
        }else {
            return false;
        }
    }

    @Override
    public List<SjsClub> selectAllClub() {
        return sjsClubDao.selectAll();
    }
}
